using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using BO;

namespace AnimalFarm.Product_manage
{
    public partial class AddBulkProductField : Telerik.WinControls.UI.RadForm
    {
        B_BulkProduct B_bulkProduct = new B_BulkProduct();
        B_FieldType B_fieldType = new B_FieldType();
        B_Field B_field = new B_Field();
        B_BulkProduct_Field B_bulkproductField = new B_BulkProduct_Field();
        public AddBulkProductField()
        {
            InitializeComponent();
        }

        private void AddBulkProductField_Load(object sender, EventArgs e)
        {
            loadBulkProduct();
            loadFieldType();
        }

        //load field type
        private void loadFieldType()
        {
            DataTable aTable = B_fieldType.GetAllFieldType();
            drdFieldType.DataSource = aTable;
            drdFieldType.DisplayMember = "FieldTypeName";
            drdFieldType.ValueMember = "FieldTypeID";
            drdFieldType.Text = "(Select field type)";
        }

        //load bulk product
        private void loadBulkProduct()
        {
            DataTable aTable = B_bulkProduct.GetAllBulkProduct();
            drdBulkProduct.DataSource = aTable;
            drdBulkProduct.DisplayMember = "BulkProductName";
            drdBulkProduct.ValueMember = "BulkProductID";
            drdBulkProduct.Text = "(Select a bulk product)";
        }

        private void drdFieldType_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            try
            {
                if (drdFieldType.SelectedIndex != -1)
                {
                    int fieldTypeID = int.Parse(drdFieldType.SelectedValue.ToString());
                    DataTable aTable = B_field.GetAllFieldByFieldType(fieldTypeID);
                    drdField.DataSource = aTable;
                    drdField.DisplayMember = "FieldName";
                    drdField.ValueMember = "FieldID";
                    drdField.Text = "(Select a field)";
                }
            }
            catch (Exception ex) { }
        }

        private void drdField_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            try
            {
                if (drdField.SelectedIndex != -1)
                {
                    int fieldID = int.Parse(drdField.SelectedValue.ToString());
                    Field_Info info = B_field.GetOneField(fieldID);
                    lblLocation.Text = info.Location;
                }
            }
            catch (Exception ex) { }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnAddDairyproduct_Click(object sender, EventArgs e)
        {
            try
            {
                int fieldID, bulkProductID, quanlity=0;
                DateTime time;
                string productName;
                fieldID = int.Parse(drdField.SelectedValue.ToString());
                bulkProductID = int.Parse(drdBulkProduct.SelectedValue.ToString());
                try
                {
                    quanlity = int.Parse(txtQuanlity.Text);
                }
                catch (Exception ex) { quanlity = 0; }
                time = dtpTime.Value;
                productName = txtProductName.Text;

                BulkProduct_Field_Info info = new BulkProduct_Field_Info();
                info.BulkProductID = bulkProductID;
                info.FieldID = fieldID;
                info.HarvestTme = time;
                info.ProductName = productName;
                info.Quantity = quanlity;

                bool isOk = B_bulkproductField.CreateOneBulkProduct_Field(info);
                if (isOk)
                {
                    MessageBox.Show(this, "Product added successful", "Product adding", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    resetFields();
                }
                else
                {
                    MessageBox.Show(this, "Product adding is fail", "Product adding", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) { }
        }

        private void resetFields()
        {
            txtProductName.Text = "";
            txtQuanlity.Text = "";
            txtProductName.Focus();
        }
    }
}
